import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;


public class Veranstaltungsverwaltung extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JTable jTable1 = new JTable(100, 5);
    private DefaultTableModel jTable1Model = (DefaultTableModel) jTable1.getModel();
    private JScrollPane jTable1ScrollPane = new JScrollPane(jTable1);
    private JList listRaum;
    private JList listFilm;
    private JFormattedTextField txtDatum;
    private JFormattedTextField txtUhrzeit;
    private Database db;
    private User user;
    private String raumbezeichnung,filmtitel;

	public Veranstaltungsverwaltung(Database db, User userObj) throws SQLException {
		
		jTable1.setFont(new Font("Verdana", Font.PLAIN, 12));
		
		this.db = db;
		this.user =userObj;
		
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 870, 620);
	    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
	    int x = (d.width - getSize().width) / 2;
	    int y = (d.height - getSize().height) / 2;
	    setLocation(x, y);
	    
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenuItem mntmKinoverwaltung = new JMenuItem("Kinoverwaltung");
		mntmKinoverwaltung.setFont(new Font("Verdana", Font.PLAIN, 14));
		mntmKinoverwaltung.addActionListener(this);
		menuBar.add(mntmKinoverwaltung);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(51, 153, 204));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblTakeASeat = new JLabel("Take A Seat - Veranstaltungsverwaltung");
		lblTakeASeat.setForeground(Color.WHITE);
		lblTakeASeat.setFont(new Font("Verdana", Font.BOLD, 36));
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBackground(new Color(51, 153, 204));
		tabbedPane.setFont(new Font("Verdana", Font.PLAIN, 14));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, 820, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblTakeASeat, GroupLayout.PREFERRED_SIZE, 834, GroupLayout.PREFERRED_SIZE)))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblTakeASeat, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, 452, Short.MAX_VALUE)
					.addContainerGap())
		);
		
		JPanel panel = new JPanel();
		tabbedPane.addTab("Veranstaltung anlegen", null, panel, null);
		
		JLabel lblRaumAuswhlen = new JLabel("Raum ausw\u00E4hlen");
		lblRaumAuswhlen.setFont(new Font("Verdana", Font.BOLD, 14));
		
		JLabel lblFilmAuswhlen = new JLabel("Film ausw\u00E4hlen");
		lblFilmAuswhlen.setFont(new Font("Verdana", Font.BOLD, 14));
		
		ResultSet rs = db.getData("SELECT RaumID, Bezeichnung FROM raum");
		ArrayList <String> raumliste = new ArrayList<String>();
		ArrayList <Integer> raumlisteInt = new ArrayList<Integer>();
		while(rs.next()){
		
			raumliste.add(rs.getString(2));
			raumlisteInt.add(Integer.parseInt(rs.getString(1)));
		}
	int anzahl=0;
		for(int i=0;i<raumliste.size();i++){
			if(raumlisteInt.get(i)%100==0)
				anzahl++;
		}
		
		
		String raumvektor[] = new String [anzahl];
		int a=0;
		for(int i=0;i<raumliste.size();i++){
			if(raumlisteInt.get(i)%100==0){
			raumvektor[a]=raumliste.get(i);
			a++;
		}
			}
		listRaum = new JList(raumvektor);
		listRaum.setFont(new Font("Verdana", Font.PLAIN, 12));
		listRaum.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		ResultSet rs2 = db.getData("SELECT Filmtitel FROM filme");
		ArrayList <String> filmliste = new ArrayList<String>();
		while(rs2.next()){
			filmliste.add(rs2.getString(1));
		}
		String filmvektor[] = new String [filmliste.size()];
		for(int i=0;i<filmliste.size();i++)
			filmvektor[i]=filmliste.get(i);
		listFilm = new JList(filmvektor);
		listFilm.setFont(new Font("Verdana", Font.PLAIN, 12));
		listFilm.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		JLabel lblDatum = new JLabel("Datum");
		lblDatum.setFont(new Font("Verdana", Font.BOLD, 14));
		
		txtDatum = new JFormattedTextField();
		txtDatum.setFont(new Font("Verdana", Font.PLAIN, 12));
		
		JLabel lblUhrzeit = new JLabel("Uhrzeit");
		lblUhrzeit.setFont(new Font("Verdana", Font.BOLD, 14));
		
		txtUhrzeit = new JFormattedTextField();
		txtUhrzeit.setFont(new Font("Verdana", Font.PLAIN, 12));
		txtUhrzeit.setText("");
		
		JButton btnVeranstaltungAnlegen = new JButton("Veranstaltung anlegen");
		btnVeranstaltungAnlegen.setFont(new Font("Verdana", Font.PLAIN, 14));
		btnVeranstaltungAnlegen.addActionListener(this);
		
		JLabel lblAnlegenErfolgreich = new JLabel("Das Anlegen der Veranstaltung war ");
		lblAnlegenErfolgreich.setVisible(false);
		
		lblAnlegenErfolgreich.setForeground(Color.BLACK);
		lblAnlegenErfolgreich.setFont(new Font("Verdana", Font.BOLD, 12));
		
		JLabel lblErfolg = new JLabel("erfolgreich");
		lblErfolg.setVisible(false);
		lblErfolg.setForeground(new Color(0, 204, 0));
		lblErfolg.setFont(new Font("Verdana", Font.BOLD, 14));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(59)
							.addComponent(lblRaumAuswhlen)
							.addGap(125)
							.addComponent(lblFilmAuswhlen))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(28)
							.addComponent(listRaum, GroupLayout.PREFERRED_SIZE, 195, GroupLayout.PREFERRED_SIZE)
							.addGap(45)
							.addComponent(listFilm, GroupLayout.PREFERRED_SIZE, 216, GroupLayout.PREFERRED_SIZE)
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel.createSequentialGroup()
									.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
										.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup()
											.addGap(38)
											.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
												.addComponent(lblUhrzeit)
												.addComponent(lblDatum))
											.addGap(18)
											.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
												.addComponent(txtUhrzeit)
												.addComponent(txtDatum, GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
												.addComponent(btnVeranstaltungAnlegen, GroupLayout.PREFERRED_SIZE, 201, GroupLayout.PREFERRED_SIZE)))
										.addGroup(gl_panel.createSequentialGroup()
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(lblAnlegenErfolgreich, GroupLayout.PREFERRED_SIZE, 258, GroupLayout.PREFERRED_SIZE)))
									.addPreferredGap(ComponentPlacement.RELATED, 6, Short.MAX_VALUE))
								.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(lblErfolg)
									.addGap(96)))))
					.addGap(18))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(30)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(lblRaumAuswhlen)
						.addComponent(lblFilmAuswhlen))
					.addGap(5)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(listRaum, GroupLayout.PREFERRED_SIZE, 363, GroupLayout.PREFERRED_SIZE)
						.addComponent(listFilm, GroupLayout.PREFERRED_SIZE, 363, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(5)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblDatum)
								.addComponent(txtDatum, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(18)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblUhrzeit)
								.addComponent(txtUhrzeit, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(btnVeranstaltungAnlegen, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)
							.addGap(44)
							.addComponent(lblAnlegenErfolgreich, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblErfolg)))
					.addGap(41))
		);
		panel.setLayout(gl_panel);
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("Veranstaltung \u00E4ndern", null, panel_1, null);
		
		jTable1.getColumnModel().getColumn(0).setHeaderValue("ID");
		jTable1.getColumnModel().getColumn(1).setHeaderValue("Raum");
		jTable1.getColumnModel().getColumn(2).setHeaderValue("Film");
		jTable1.getColumnModel().getColumn(3).setHeaderValue("Datum");
		jTable1.getColumnModel().getColumn(4).setHeaderValue("Uhrzeit");
		
		 ResultSet rsTable = db.getData("SELECT * FROM veranstaltungen");
		    
		    int row=0;
		    	    
		    while(rsTable.next()){
		    	System.out.println("Durchlauf " + (row+1));
		    	    jTable1.setValueAt(rsTable.getString(1),row,0);
		    	    ResultSet rsRaum = db.getData("SELECT Bezeichnung FROM raum WHERE RaumID = " + Integer.parseInt(rsTable.getString(4)));
		    	    rsRaum.next();
		    	    raumbezeichnung = rsRaum.getString(1);
		    	    jTable1.setValueAt(raumbezeichnung, row, 1);
		    	    ResultSet rsFilm = db.getData("SELECT Filmtitel FROM filme WHERE Filmnummer = " + Integer.parseInt(rsTable.getString(5)));
		    	    rsFilm.next();
		    	    filmtitel = rsFilm.getString(1);
		    	    jTable1.setValueAt(filmtitel ,row,2);
		    	    jTable1.setValueAt(rsTable.getString(2),row,3);
		    	    jTable1.setValueAt(rsTable.getString(3),row,4);
		    	   // System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4));
		    	    row++;
		    	    
		    }
		
		
		
		JButton btnnderungenbernehmen = new JButton("\u00C4ndern");
		btnnderungenbernehmen.setFont(new Font("Verdana", Font.PLAIN, 14));
		btnnderungenbernehmen.addActionListener(this);
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGap(7)
					.addComponent(jTable1ScrollPane, GroupLayout.PREFERRED_SIZE, 581, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnnderungenbernehmen, GroupLayout.PREFERRED_SIZE, 206, GroupLayout.PREFERRED_SIZE)
					.addGap(20))
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGap(7)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
						.addComponent(btnnderungenbernehmen, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
						.addComponent(jTable1ScrollPane, GroupLayout.PREFERRED_SIZE, 436, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(14, Short.MAX_VALUE))
		);
		panel_1.setLayout(gl_panel_1);
		contentPane.setLayout(gl_contentPane);
		
		setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent e){
		
		if(e.getActionCommand()=="\u00C4ndern"){
			System.out.println("ActionPerformed angelangt Event korrekt");
			int id = Integer.parseInt((String) jTable1.getValueAt(jTable1.getSelectedRow(), 0));
			try {
				new VeranstaltungChange(id, db, user);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			dispose();
		}
		
		if (e.getActionCommand()=="Kinoverwaltung"){
			dispose();
			try {
				new Kinoverwaltung(db, user);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}//.main(null);
		}
		if (e.getActionCommand()=="Veranstaltung anlegen"){
			System.out.println("Veranstaltung wird angelegt...bitte warten");
			String raumID = null;
			String filmID = null;
			String datum = txtDatum.getText();                //Datum
			String zeit  = txtUhrzeit.getText();                //Zeit
			System.out.println("Zeit und Datum korrekt");
			
			raumID = (String) listRaum.getSelectedValue();
			filmID = (String) listFilm.getSelectedValue();
			
			System.out.println("raum und Film: " + raumID + " und " + filmID);
			
			
			ResultSet rsRaum = null;
			try {
				rsRaum = db.getData("SELECT RaumID FROM raum WHERE Bezeichnung LIKE '" + raumID + "'");
			} catch (SQLException e3) {
				// TODO Auto-generated catch block
				e3.printStackTrace();
			}
			try {
				rsRaum.next();
			} catch (SQLException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			try {
				raumID = rsRaum.getString(1);                   //RaumID
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			ResultSet rsFilm = null;
			try {
				rsFilm = db.getData("SELECT filmnummer FROM filme WHERE filmtitel LIKE '" + filmID + "'");
			} catch (SQLException e3) {
				// TODO Auto-generated catch block
				e3.printStackTrace();
			}
		    try {
				rsFilm.next();
			} catch (SQLException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
		    try {
				filmID = rsFilm.getString(1);                     //FilmID
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		    
		    try {    
			System.out.println("RaumID = " + raumID + " FilmID = " + filmID);
			Raum room = new ConvertTo().raum(db, Integer.parseInt(raumID));
			System.out.println("Sind wir schon hier im Raum " + room.getBezeichnung());
			int tempRaumID = db.findRaumTempID(Integer.parseInt(raumID));
			db.setRoom(room.getBezeichnung(),room.getReihen(),room.getAnzahl_je_reihe(),room.getReihenLoge(),room.getAnzahl_je_reiheLoge(),tempRaumID);	
			db.setVeranstaltung(datum, zeit, tempRaumID, filmID);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		    System.out.println("Veranstaltung wurde angelegt");
		}
		
	}
}
